# Maintainer: Hurcan Solter <hsolter@gmail.com>

_realname=armadillo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.900.1
pkgrel=2
pkgdesc="C++ linear algebra library (mingw-w64)"
arch=('any')
url="https://arma.sourceforge.io/"
license=(MPL2)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
install=${_realname}-${CARCH}.install
source=(https://sourceforge.net/projects/arma/files/${_realname}-${pkgver}.tar.xz
        0001-mingw-config-fix.patch)
sha256sums=('53d7ad6124d06fdede8d839c091c649c794dae204666f1be0d30d7931737d635'
            '772719e60eb2970ecb37844382dbcb6d0439f949c5080f9865798115640b612a')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-mingw-config-fix.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DLAPACK_NAMES=openblas \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  install -d ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}
  install -m644 "${srcdir}/${_realname}-${pkgver}/"*{.html,.png,.pdf,README.md} \
    "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"

  # Remove builders local installation path
  local _prefix_win=$(cygpath -m ${MINGW_PREFIX})
  sed -s "s|${_prefix_win}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/include/armadillo_bits/config.hpp"
}
